#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"

char* create_vfs(char vfs_label[], int size_create)
{
	char result[100];
	if(vfs_label==NULL || size_create==0)
	{
		strcpy(result,"createvfs_FAILURE ");
		strcat(result,ERR_VFS_CREATE_00);
		sprintf(str, "%s",result);
		return str;
	//return ERR_VFS_CREATE_00; // VFS_INSUFFICIENT_ARGUMENTS
	}

	FILE *create_pointer;
	int a;
	int i;
	int fds;
	char ab[50];
	main_header mainheadervar;
	file_descriptor f_d,f_d1,f_d2;
	freelist f_l,f_l1,f_l2;
	blocklist b_l;
	f_l.a =0;
	int ij=0;
	int flag = 0;
	fds = size_create*1.2;
	//printf("%d",fds);
	if(size_create<1 || size_create >1024)
	{
		strcpy(result,"createvfs_FAILURE ");// invalid size_create
		strcat(result,ERR_VFS_CREATE_04);
		sprintf(str, "%s",result);
		return str;
		//return ERR_VFS_CREATE_04; // invalid size_create
	}	
	if(strlen(vfs_label)>30)
	{
		strcpy(result,"createvfs_FAILURE ");//vfs_label_too_large
		strcat(result,ERR_VFS_CREATE_05);
		sprintf(str, "%s",result);
		return str;
		//return ERR_VFS_CREATE_05; //vfs_label_too_large
	}
	
	while(vfs_label[ij]!='\0')
	{
		if(vfs_label[ij]=='/')
			flag = 1;
			ij++;
	}
	if(flag == 1)
	{
		strcpy(result,"createvfs_FAILURE ");// invalid size_create	
		strcat(result,ERR_VFS_CREATE_04);
		sprintf(str, "%s",result);
		return str;//return ERR_VFS_CREATE_04;
	}
	strcpy(ab,vfs_label);
	strcat(ab,".dat");
	create_pointer = fopen(ab, "ab");
	int xy = ftell(create_pointer);
	if( xy != 0)	
	{
		strcpy(result,"createvfs_FAILURE ");//data_file_already_exists
			strcat(result,ERR_VFS_CREATE_01);
			sprintf(str, "%s",result);
			return str;
			//return ERR_VFS_CREATE_01; //data_file_already_exists
	}	
	fclose(create_pointer);		


	strcpy(mainheadervar.label,vfs_label);
	mainheadervar.no_used_file_descriptors = 1;
	mainheadervar.max_no_file_descriptors =fds;
	mainheadervar.max_no_blocks = size_create;
	mainheadervar.no_used_blocks=0;
	
	strcpy(ab,vfs_label);
	strcat(ab,".dat");
	FILE *fp = fopen(ab, "wb");
	if(fp==NULL)
	{
		strcpy(result,"createvfs_FAILURE ");
			strcat(result,ERR_VFS_CREATE_02);
			sprintf(str, "%s",result);
			return str;
		//return ERR_VFS_CREATE_02; // cannot_create_data_file
	}
	int x=ftell(fp);
	
	fwrite(&mainheadervar,sizeof(main_header),1,fp);
	x = ftell(fp);
	//printf("mhs is %d in create",x);
	
	strcpy(f_d1.file_name,"home");
	strcpy(f_d1.location_path,"/home");
	strcpy(f_d1.file_type,"folder");

	fwrite(&f_d1,sizeof(file_descriptor),1,fp);
	x = ftell(fp);
	
	for(i=1;i<fds;i++)
	{
		fwrite(&f_d,sizeof(file_descriptor),1,fp);	
	}
	x = ftell(fp);
	//printf("fds is %d in create",x);

	int l;
	f_l1.a = 1;
	fwrite(&f_l1,sizeof(freelist),1,fp);
	
	for(l=1;l<fds;l++)
	{
			fwrite(&f_l,sizeof(freelist),1,fp);
	}
	x = ftell(fp);
//	printf("after f_l is %d in create",x);
	b_l.b=0;
	for(l=0;l<size_create;l++)
	{
		//printf("%d",b_l.b);
			fwrite(&b_l,sizeof(freelist),1,fp);
	}
	x = ftell(fp);
	//printf("mhs is %d in create",x);
	mainheadervar.headersize=x;
	rewind(fp);
	fwrite(&mainheadervar,sizeof(main_header),1,fp);
	fseek(fp,x,SEEK_SET);
	for(i=0;i<fds;i++)
	{
		fwrite("0",1,1024,fp);
	}
	
	x = ftell(fp);
	
fclose(fp);
return "createvfs_SUCCESS";

//free(f_l);
//free(f_d);
}
	
